﻿using System;
using System.IO;
using System.Windows.Forms;

namespace FileMergerApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFile1.Text = openFileDialog1.FileName;
            }
        }

        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtFile2.Text = openFileDialog1.FileName;
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtFile1.Text) || string.IsNullOrWhiteSpace(txtFile2.Text))
            {
                MessageBox.Show("请先选择两个文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                // 读取文件内容
                string content1 = File.ReadAllText(txtFile1.Text);
                string content2 = File.ReadAllText(txtFile2.Text);

                // 合并内容
                string mergedContent = content1 + Environment.NewLine + content2;

                // 确保Data目录存在
                string dataDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                if (!Directory.Exists(dataDir))
                {
                    Directory.CreateDirectory(dataDir);
                }

                // 生成新文件名
                string newFileName = $"merged_{DateTime.Now:yyyyMMddHHmmss}.txt";
                string newFilePath = Path.Combine(dataDir, newFileName);

                // 写入新文件
                File.WriteAllText(newFilePath, mergedContent);

                MessageBox.Show($"文件合并成功!\n保存路径: {newFilePath}", "成功",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"合并文件时出错:\n{ex.Message}", "错误",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
